package com.source.lightfowl.basedata.controller;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.PoultryCategoryDownloadDTO;
import com.source.lightfowl.DTO.PoultryCategoryUploadDTO;
import com.source.lightfowl.VO.PoultryCategoryVO;
import com.source.lightfowl.basedata.entity.PoultryCategory;
import com.source.lightfowl.basedata.entity.SlaughterHouse;
import com.source.lightfowl.basedata.service.PoultryCategoryService;
import com.source.lightfowl.basedata.service.SlaughterHouseService;
import com.source.lightfowl.basedata.service.UploadExcelService;
import com.source.lightfowl.handler.MyException;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:19
 */
@Api(value = "禽类信息管理",tags = "禽类信息管理")
@RestController
@CrossOrigin
@RequestMapping
public class PoultryCategoryController {
    @Autowired
    private PoultryCategoryService poultryCategoryService;
    @Autowired
    private SlaughterHouseService slaughterHouseService;

    //查找全部
    @ApiOperation(value = "查询全部数据")
    @GetMapping("/poultryCategory")
    public MyResult findAll(){
        List<PoultryCategoryVO> all = poultryCategoryService.findAll();
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    /*
        疑问：如果参数的页面数超过数据库的数据  要怎么显示？
     */
    @ApiOperation(value = "分页查询 两个参数 页数 个数/页")
    @GetMapping("/poultryCategoryFindAllPage")
    public MyResult findAllPage(int page,int pageNum){

        Page<PoultryCategory> all = poultryCategoryService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    /*
        疑问：如果查找的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/poultryCategory/id")
    public MyResult findById(String id){
        PoultryCategoryVO house = poultryCategoryService.findOneById(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    @ApiOperation(value = "根据编号查询")
    @GetMapping("/poultryCategory/idnumber")
    public MyResult findByIdNumber(String id){
        PoultryCategory house = poultryCategoryService.findByIdNumber(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    //增加 修改
    @ApiOperation(value = "增加/修改 数据 ")
    @PostMapping("/poultryCategory")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(PoultryCategory poultryCategory){

        poultryCategoryService.save(poultryCategory);

        return MyResult.build(0,"成功");


    }

    //删除
    /*
        疑问：如果删除的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation("根据id删除数据")
    @DeleteMapping("/poultryCategory/id")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(String id){

        try {
            poultryCategoryService.deleteById(id);
            return MyResult.build(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.build(1,"删除失败");
        }


    }


    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/poultryCategoryDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的

        poultryCategoryService.deleteBatch(ids);
        //List<SlaughterHouse> all = slaughterHouseService.findAll();
        return MyResult.build(0,"看是否少了删除的id");

    }

    @ApiOperation(value = "禽类Excel数据上传")
    @PostMapping("/poultryCategory/uploadExcel")
    @ResponseBody
    public MyResult uploadExcel(@RequestParam(value = "file")MultipartFile file, HttpServletRequest request){
        String fileName = file.getOriginalFilename(); //文件名
        String fileSuffix = fileName.substring(fileName.length()-5); //.xlsx
        String fileSuffix1 = fileName.substring(fileName.length()-4); //.xls
        if (!fileSuffix.equals(".xlsx")&&!fileSuffix1.equals(".xls")){
            return MyResult.build(500,"文件格式错误");
        }
        try {
            EasyExcel.read(file.getInputStream(), PoultryCategoryUploadDTO.class,new UploadExcelListener(poultryCategoryService,request)).sheet().doRead();
        }catch (IOException e){
            e.printStackTrace();
            throw new MyException(500,"文件错误");
        }
        return MyResult.build(200,"上传成功");
    }

    @ApiOperation(value = "禽类Excel下载",produces = "application/octet-stream")
    @GetMapping("/poultryCategory/downloadExcel")
    @ResponseBody
    public MyResult downloadExcel(HttpServletResponse response){
        try {
            DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
            downloadExcelHandler.download(response,poultryCategoryService, PoultryCategoryDownloadDTO.class);
        }catch (Exception e){
            return MyResult.no();
        }
        return MyResult.ok();
    }
}
